package com.ruoyi.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.web.domain.ContactPhone;
import com.ruoyi.web.mapper.ContactPhoneMapper;
import com.ruoyi.web.service.IContactPhoneService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ContactPhoneServiceImpl extends ServiceImpl<ContactPhoneMapper, ContactPhone> implements IContactPhoneService {

    @Autowired
    private ContactPhoneMapper contactPhoneMapper;

    @Override
    public List<ContactPhone> getList(ContactPhone contactPhone) {
        return contactPhoneMapper.selectContactPhoneList(contactPhone);
    }

    @Override
    public Object getPhoneNum() {
        List<ContactPhone> contactPhones = contactPhoneMapper.selectList(new LambdaQueryWrapper<ContactPhone>().orderByDesc(ContactPhone::getUpdateTime));
        ContactPhone phone = contactPhones.get(0);
        return phone.getPhoneNum();
    }

    @Override
    public Object getEmail() {
        List<ContactPhone> contactPhones = contactPhoneMapper.selectList(new LambdaQueryWrapper<ContactPhone>().orderByDesc(ContactPhone::getUpdateTime));
        ContactPhone phone = contactPhones.get(0);
        return phone.getEmail();
    }

    @Override
    public void update(ContactPhone contactPhone) {
        List<ContactPhone> contactPhones = contactPhoneMapper.selectList(null);
        ContactPhone phone = contactPhones.get(0);
        phone.setPhoneNum(contactPhone.getPhoneNum());
        phone.setEmail(contactPhone.getEmail());
        Long userId = SecurityUtils.getUserId();
        phone.setModifier(userId);
        phone.setUpdateTime(LocalDateTime.now());
        contactPhoneMapper.updateById(phone);
    }
}
